﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace AI.Graph {


    /// <summary>
    /// Array Utility. Unity's class is an Editor only class.
    /// </summary>
    public static class ArrayUtility {

        public static void Add<T> (ref T[] array, T item) {
            Array.Resize<T> (ref array, array.Length + 1);
            array[array.Length - 1] = item;
        }


        public static void AddRange<T> (ref T[] array, T[] items) {
            int length = array.Length;
            Array.Resize<T> (ref array, array.Length + items.Length);
            for (int index = 0; index < items.Length; ++index)
                array[length + index] = items[index];
        }


        public static void Insert<T> (ref T[] array, int index, T item) {
            ArrayList arrayList = new ArrayList ();
            arrayList.AddRange (array);
            arrayList.Insert (index, item);
            array = arrayList.ToArray (typeof (T)) as T[];
        }


        public static void Remove<T> (ref T[] array, T item) {
            List<T> objList = new List<T> (array);
            objList.Remove (item);
            array = objList.ToArray ();
        }


        public static bool Contains<T> (T[] array, T item) {
            return new List<T> (array).Contains (item);
        }


        public static void Clear<T> (ref T[] array) {
            Array.Clear (array, 0, array.Length);
            Array.Resize<T> (ref array, 0);
        }

    }


}